﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyDiemHocSinh
{
    public partial class TrangDangNhap : System.Web.UI.Page
    {
        String tendn, quyen;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string chuoiketnoi = "Server=.\\SqlExpress;Integrated Security=True;Database=QuanLyDiem";
            SqlConnection ketnoi = new SqlConnection(chuoiketnoi);

            SqlDataReader rdr = null;
            SqlCommand cmd = new SqlCommand("select * from NguoiDung", ketnoi);
            try
            {
                ketnoi.Open();

                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    tendn = (string)rdr["MaNguoiDung"];
                    String matkhau = (string)rdr["MatKhau"];
                    quyen = (string)rdr["Quyen"];
                    if (string.Compare(tendn, tbtendn.Text) == 0)
                    {
                        if (string.Compare(matkhau, tbmatkhau.Text) == 0)
                        {
                            Session["tendn"] = tendn;
                            Session["quyendn"] = quyen;
                            if(String.Compare(quyen,"Admin")==0)
                            Response.Redirect("~/Admin.aspx");
                            else
                                if(String.Compare(quyen,"HocSinh")==0)
                                    Response.Redirect("~/HocSinh.aspx");

                            break;
                        }
                        else lbketnoi.Text = "Sai mật khẩu!";
                    }
                    else lbketnoi.Text = "Không tồn tại tài khoản!"+ tendn +"." + tbtendn.Text+ matkhau+quyen;
                }
            }
            catch (Exception ex)
            {
                lbketnoi.Text = ex.ToString();
            }
        }
    }
}